import {
   Controller,
   Get,
   Post,
   Body,
   Param,
   Delete,
   UseGuards,
   ClassSerializerInterceptor,
   UseInterceptors,
   SerializeOptions
} from '@nestjs/common'
import { CommontService } from './commont.service'
import { CreateCommontDto } from './dto/create-commont.dto'
import { AuthJWT } from 'src/decorator/auth.decorator'
import { GetUserInfo } from 'src/decorator/getUserInfo.decotator'
import { User } from '@prisma/client'
import { Roles } from 'src/guards/roles.decorator'
import { CommontPolicy } from './commont.policy'
import { CommontEntity } from 'src/entitys/commont.entitys'

@Controller('commont/:aid')
export class CommontController {
   constructor(private readonly commontService: CommontService) {}

   @Post()
   @AuthJWT()
   create(@Body() createCommontDto: CreateCommontDto, @Param('aid') aid: number, @GetUserInfo() user: User) {
      return this.commontService.create(createCommontDto, aid, user.id)
   }

   @Get(':cid')
   findOne(@Param('cid') id: number) {
      return this.commontService.findOne(+id)
   }

   @UseInterceptors(ClassSerializerInterceptor)
   @SerializeOptions({
      excludePrefixes: ['password']
   })
   @Get()
   @SerializeOptions({ groups: [] })
   async findAll(@Param('aid') id: number): Promise<CommontEntity[]> {
      return (await this.commontService.findAll(id)).map((item) => new CommontEntity(item))
   }

   @Delete(':id')
   @Roles(CommontPolicy, 'commont')
   @UseGuards()
   @AuthJWT()
   removeCommont(@Param('id') id: string) {
      return this.commontService.removeCommont(+id)
   }
}
